package com.app.twth.ui.cyclecount.location;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;


import com.app.twth.databinding.ItemCyclecountLocationListBinding;
import com.app.twth.interfaces.MyOnClickListener;

import java.util.ArrayList;
import java.util.List;


public class CCLocationAdapter extends RecyclerView.Adapter<CCLocationAdapter.MyViewHolder> {
    private LayoutInflater mInflater;
    private MyOnClickListener myOnClickListener;
    private List<CycleCountLocationQr.SkuListDTO> data = new ArrayList<>();

    public CCLocationAdapter(Context mContext, List<CycleCountLocationQr.SkuListDTO> data, MyOnClickListener myOnClickListener) {
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.myOnClickListener = myOnClickListener;
    }

    public void setData(List<CycleCountLocationQr.SkuListDTO> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemCyclecountLocationListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemCyclecountLocationListBinding itemBinding;

        public MyViewHolder(ItemCyclecountLocationListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            CycleCountLocationQr.SkuListDTO inventoryDTO = data.get(position);
            itemBinding.tvItem.setText(inventoryDTO.getSku() + "(" + inventoryDTO.getQty() + ")");
            if (inventoryDTO.isCycleCount()) {
                itemBinding.ivFinish.setVisibility(View.VISIBLE);
            } else {
                itemBinding.ivFinish.setVisibility(View.INVISIBLE);
            }
            itemBinding.tvInventory.setText(inventoryDTO.getIbLpn() + "");
            itemBinding.layout.setOnClickListener(v -> myOnClickListener.OnClick(position));
        }
    }
}
